import 'package:boss/models/commodity.dart';
import 'package:boss/provider/offers_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/orderDiscount/widget/offer_commodity_item.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/specialOffers/provider/special_offers_provider.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/widget/offers_form_field_common.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/custom_divier.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/picker/commodityPicker/commodity_picker.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class SpecialOffersBrowser extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.symmetric(
              vertical: 30.w,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                OffersFormFieldCommon<SpecialOffersProvider>(
                  title: '特价菜品',
                ),
                CustomDivider(
                  height: 20.w,
                ),
                Padding(
                  padding: EdgeInsets.symmetric(
                    horizontal: 55.w,
                  ),
                  child: Column(
                    children: <Widget>[
                      Consumer<SpecialOffersProvider>(
                        builder: (context, specialOffersProvider, child) {
                          return BaseTextFormField(
                            prefixText: '菜品折扣率',
                            hintText: '例：打九折，填写90',
                            suffixText: '%',
                            maxLength: 2,
                            keyboardType: TextInputType.number,
                            inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
                            onChanged: (value) {
                              var specialOffersProvider = Provider.of<SpecialOffersProvider>(
                                context,
                                listen: false,
                              );
                              specialOffersProvider.setDiscountRate(value);
                            },
                            initialValue: specialOffersProvider.discountRate,
                          );
                        },
                      ),
                      CustomListTitle(
                        title: Consumer<SpecialOffersProvider>(
                          builder: (context, specialOffersProvider, _) {
                            return RichText(
                              text: TextSpan(text: '活动菜品', style: TextStyleConstant().normal_1_28, children: [
                                TextSpan(
                                  text: '（${specialOffersProvider.commoditys.length}）',
                                  style: TextStyleConstant().origin_24,
                                ),
                              ]),
                            );
                          },
                        ),
                        isHideDivider: true,
                        trailing: LabelButton(
                          title: '+选择菜品',
                          textStyle: TextStyleConstant().blue_24,
                          onTap: () async {
                            var specialOffersProvider = Provider.of<SpecialOffersProvider>(
                              context,
                              listen: false,
                            );
                            var result = await showCommodityPicker(
                              context,
                              selectedCommodities: specialOffersProvider.commoditys,
                              isShowSku: true,
                              isShowPackage: false,
                              filterType: [Commodity.COMMODITY_TYPE_PACKAGE, Commodity.COMMODITY_TYPE_WEIGH],
                            );
                            if (result != null) {
                              specialOffersProvider.setCommoditys(result);
                            }
                          },
                        ),
                      ),
                      Consumer<SpecialOffersProvider>(
                        builder: (context, specialOffersProvider, child) {
                          return ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemBuilder: (context, index) {
                              var current = specialOffersProvider.commoditys[index];
                              return OfferCommodityItem(
                                data: current,
                                onDeleted: () {
                                  specialOffersProvider.removeCommodity(current);
                                },
                              );
                            },
                            itemCount: specialOffersProvider.commoditys.length,
                          );
                        },
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
        FixedBottomBtn(
          title: '保 存',
          onTap: () async {
            var fullOffersProvider = Provider.of<SpecialOffersProvider>(
              context,
              listen: false,
            );
            var result = await fullOffersProvider.addSpecialOffers();
            if (result) {
              var offersProvider = Provider.of<OffersProvider>(
                context,
                listen: false,
              );
              offersProvider.backstageRefresh();
              Navigator.pop(context);
            }
          },
        ),
      ],
    );
  }
}
